package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.utils.DateUtils;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @version v1.0
 * @date 2019/10/14 15:00
 * @description TODO
 **/
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {


    @Autowired   //预约设置
    private OrderSettingDao orderSettingDao;

    @Autowired   //会员
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result submit(Map map) throws Exception {
        //判断选择的日期是否能够预约
        //如果不能预约，提示用户
        String orderDate= (String) map.get("orderDate");
        String telephone = (String) map.get("telephone");

        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting= orderSettingDao.findOrderSettingByOrderDate(date);

        //判断预约日期人数是否预约满了
        if (orderSetting == null) {
            //没有预约设置
        //如果预约满了，提示用户不能预约
            return Result.error(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        int number=orderSetting.getNumber();//可预约人数
        int reservations=orderSetting.getReservations();//已预约人数
        if (number <= reservations) {
            return Result.error(MessageConstant.ORDER_FULL);
        }
        //如果没满，判断用户是否注册会员
        //已注册，预约人数+1
        //未注册，需要先注册
        Member member=memberDao.findByTelephone(telephone);
        if (member == null) {
            //没注册会员,往会员表新增会员
            member = new Member();
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber(telephone);
            member.setName((String) map.get("name"));
            member.setRegTime(new Date());

            memberDao.add(member);
        }
        //检查用户是否重复预约
        //如果重复预约，提示用户，已预约
        Order orderQueryParam = new Order();
        orderQueryParam.setMemberId(member.getId());
        orderQueryParam.setOrderDate(date);
        orderQueryParam.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        List<Order> orders=orderDao.findByCondition(orderQueryParam);
        if (orders != null && orders.size() > 0) {
            //已经预约了
            return Result.error(MessageConstant.HAS_ORDERED);
        }
        //没有预约过提交数据到数据库    预约设置（已预约人数+1） t_order新增数据
        orderSetting.setReservations(orderSetting.getReservations()+1);

        orderSettingDao.updateReservationsByDate(orderSetting);

        Order order = new Order();
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        order.setOrderDate(date);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setMemberId(member.getId());

        //新增预约信息
        orderDao.add(order);

        return Result.success(MessageConstant.ORDER_SUCCESS, order);
    }


    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }
}